﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.BikeManage;
using DataAccess.Common;
using Ninject;
using WinForm.Helper;

namespace WinForm.Dialog
{
    public partial class EditLockAddress : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly ILockArea _lockArea = Kernel.Get<ILockArea>();
        private readonly ILockAddress _lockAddress = Kernel.Get<ILockAddress>();
        private readonly ILookUpDataService _lookUpDataService = Kernel.Get<LookUpDataService>();

        public EditLockAddress()
        {
            InitializeComponent();
        }

        public EditLockAddress(int addressid)
        {
            InitializeComponent();
            lbAddressId.Text = addressid.ToString();
            if (addressid > 0)
            {
                var area = _lockAddress.GetLockAddress(addressid);
                txtAddressName.Text = area.Obj.name;
                txtThreshold.Text = area.Obj.threshold.ToString();
                cmbArea.Text = area.Obj.areaname;
                cmsStatus.Text = area.Obj.status.ToString();
            }
        }

        private void EditLockAddress_Load(object sender, EventArgs e)
        {
            InitializeAreas();
            InitializeStatuses();
        }

        /// <summary>
        /// 初始化区域
        /// </summary>
        private void InitializeAreas()
        {
            ResultInfo<IList<jsh_Area>> areas = _lockArea.GetLockArea(0);
            if (areas.ResultNumber == ResultEnum.Success)
            {
                cmbArea.DataSource = areas.Obj;
                cmbArea.DisplayMember = "Area";
                cmbArea.ValueMember = "id";
            }
        }

        /// <summary>
        /// 初始化状态
        /// </summary>
        private void InitializeStatuses()
        {
            List<IdNameObject> status = _lookUpDataService.GetLockaddressStatus();
            cmsStatus.DataSource = status;
            cmsStatus.DisplayMember = "Name";
            cmsStatus.ValueMember = "Name";
        }

        private void btnSaveLockAddress_Click(object sender, EventArgs e)
        {

            if (String.IsNullOrEmpty(txtAddressName.Text) || string.IsNullOrWhiteSpace(txtAddressName.Text))
            {
                MyMessageBox.ShowMessageBoxInfo("请输入停车点名称！");
                return;
            }

            int id = Converts.ConvertToInt(lbAddressId.Text);
            var address = new lockaddress
            {
                id = id,
                areaname = cmbArea.Text,
                areaid =
                    Converts.ConvertToInt(cmbArea.SelectedValue == null ? "" : cmbArea.SelectedValue.ToString()),
                name = txtAddressName.Text,
                status = cmsStatus.SelectedValue == null ? "" : cmsStatus.SelectedValue.ToString(),
                threshold = Converts.ConvertToInt(txtThreshold.Text)
            };
            ResultInfo<int> result = _lockAddress.CreateOrUpdateLockAddress(address);
            if (result.ResultNumber == ResultEnum.Success)
            {
                if (!string.IsNullOrEmpty(lbAddressId.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("修改停车点信息成功！");
                }
                if (string.IsNullOrEmpty(lbAddressId.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("新增停车点信息成功！");
                }
                DialogResult = DialogResult.OK;
            }
            else
            {
                MyMessageBox.ShowMessageBoxInfo(result.Describe);
            }
        }

        private void btnLockAddressCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }
    }
}
